<?php
/**
* pndb the db resource connector
*/
error_reporting ( E_ALL & ~E_NOTICE);

require_once('../../config/config.php');
require_once('system/ypFakeDBUtil.class.php');
require_once('system/ypFakeLoader.class.php');
require_once('system/ypFakeObjectUtil.class.php');
require_once('system/ypFakeFormUtil.class.php');
require_once('system/ypcoredb.php');
require_once('pntables.php');
require_once('pnversion.php');
pnresource_main();



function pnresource_main(){
  
  $func = FormUtil::getPassedValue ('func', false , 'GET');
  if (function_exists('pnresource_' .$func)){
    call_user_func('pnresource_' .$func);
  }else{
    die( 'function not exists');
  }
}

function pnresource_getresource(){
  error_reporting ( E_ALL & ~E_NOTICE);

  $db = new YPDBUtil();

  $rstype = FormUtil::getPassedValue ('rstype', 'image' , 'GET'); //icon | image | video | model
  $id = FormUtil::getPassedValue ('id', false , 'GET'); //resource id
  $fieldname = FormUtil::getPassedValue ('fieldname', 'data' , 'GET'); //resource id
  $status = FormUtil::getPassedValue ('status', false , 'GET'); 
  $referer_id =  FormUtil::getPassedValue ('referer_id', false , 'GET'); 
  $path =  FormUtil::getPassedValue ('path', false , 'GET'); 
  $table = '';
  $type =false;
  $rs = null;
  $filter_field = '';
  switch($rstype){
    case 'image':
      $table = 'innoforum_resource_image';
      $tbl_prefix = 'rsi_';
      break;
  }
  if (empty($table)){
    $table = 'innoforum_resource_image';
    $tbl_prefix = 'rsi_';
  }
  if ($table && $id){
    $rs = $db->ypselectObjectById($table , $id);
  }
  if ($table && $referer_id){
    $rs = $db->ypselectObject($table, 'WHERE ' . 
          $tbl_prefix . "status = 1 AND " . 
          $tbl_prefix . "referer_id = '$referer_id' AND " . 
          $tbl_prefix . "path = '$path'"
          );
    if (!$rs){
      $rs = $db->ypselectObject($table, 'WHERE ' . 
          $tbl_prefix . 'status = 0 AND ' . 
          $tbl_prefix . "referer_id = '$referer_id' AND " .
          $tbl_prefix . "path = '$path'"
          );
    }
  }
  if ($rs){
    $len = strlen($rs[$fieldname]);
    $type = $rs['type'];
    // outputing HTTP headers
    header('Content-Length: '. $len);
    header('Content-type: ' . $type);
    
    echo $rs[$fieldname];
  }

  unset($rs);
  return true;
  
}
